package com.jake.data_structure_learning.list;

public class MyLinkedList1 {
    private Integer value;
    private MyLinkedList1 next;

    private Integer size;


    /**
     * Initialize your data structure here.
     */
    public MyLinkedList1() {
        value = null;
        next = null;
        size = 0;
    }

    /**
     * Get the value of the index-th node in the linked list. If the index is invalid, return -1.
     */
    public int get(int index) {
        if (size == 0) {
            return -1;
        }
        if (index == 0) {
            return value;
        }

        int i = 0;
        MyLinkedList1 linkedList = next;
        while (linkedList != null) {
            if (++i == index) {
                return linkedList.value;
            }
            linkedList = linkedList.next;
        }
        return -1;
    }

    /**
     * Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list.
     */
    public void addAtHead(int val) {
        MyLinkedList1 second = new MyLinkedList1();
        second.value = this.value;
        second.next = this.next;

        this.value=val;
        this.next = second;
        size++;
    }

    /**
     * Append a node of value val to the last element of the linked list.
     */
    public void addAtTail(int val) {
        if (size == 0) {
            this.value = val;
            size++;
            return;
        }

        MyLinkedList1 end = new MyLinkedList1();
        end.value = val;

        if (size == 1) {
            next = end;
            size++;
            return;
        }

        MyLinkedList1 linkedList = next;
        while (linkedList.next != null) {
            linkedList = linkedList.next;
        }


        linkedList.next = end;
        size++;
    }

    /**
     * Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted.
     */
    public void addAtIndex(int index, int val) {
        if (index > size - 1) {
            return;
        }
        int i = 0;
        MyLinkedList1 linkedList = next;
        while (linkedList != null) {
            if (i++ == index - 1) {
                MyLinkedList1 insert = new MyLinkedList1();
                insert.value = val;
                if (linkedList.next != null) {
                    MyLinkedList1 current = linkedList.next;
                    linkedList.next = insert;
                    insert.next = current;
                } else {
                    linkedList.next = insert;
                }
            }
        }
    }

    /**
     * Delete the index-th node in the linked list, if the index is valid.
     */
    public void deleteAtIndex(int index) {
        if (index > size - 1) {
            return;
        }
        int i = 0;
        MyLinkedList1 linkedList = next;
        while (linkedList != null) {
            if (i++ == index - 1) {
                if (linkedList.next != null) {
                    MyLinkedList1 after = linkedList.next.next;
                    linkedList.next = after;
                } else {
                    linkedList.next = null;
                }
            }
        }
    }
}
